/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.dp.model;

import com.dp.utils.BaseException;
import com.dp.utils.HibernateUtil;
import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;

/**
 *
 * @author hisik
 * @param <T>
 */
public class AbstractDao<T extends BaseEntity> implements IBaseDao<T> {

    private final Class<T> clazz;

    public AbstractDao() {
        Type genericSuperClass = getClass().getGenericSuperclass();

        // Get the generic super class of the super class if it's a cglib proxy
        if (getClass().getName().contains("$$EnhancerByCGLIB$$")) {
            genericSuperClass = getClass().getSuperclass().getGenericSuperclass();
        }

        this.clazz = (Class<T>) ((ParameterizedType) genericSuperClass).getActualTypeArguments()[0];
    }

    @Override
    public final List<T> findAll() throws BaseException {
        
        Session session = null;
        try {
        
            session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();

            Query query = session.createQuery("select a from " + clazz.getName() + " a ");            

            return (List<T>)query.list();

        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            if(session != null) {
                session.flush();
                session.close();
            }
        }
        
        return null;
    }
    
    public final T findByObjId(Serializable id) throws BaseException {

        if (id == null) return null;
        
        T instance = null;
        Session session = null;
        try {
        
            session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();

            Query hql = session.createQuery(" from " + clazz.getName() + " where objid = :id ");
            hql.setParameter("id", id);

            List list = hql.list();
            if (list != null && !list.isEmpty()) {
                instance = (T) list.get(0);
            }

        } catch (Exception ex) {
            
        } finally {
            if(session != null) {
                session.flush();
                session.close();
            }
        }
        
        return instance;
    }

    public final void saveOrUpdate(T instance) throws BaseException {
        
        Session session = null;
        try {
            
            session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
        
            session.saveOrUpdate(instance);
            session.getTransaction().commit();
                    
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            if(session != null) {
                session.flush();
                session.close();
            }
        }
    }
    
    public final void delete(T instance) throws BaseException {
        
        Session session = null;
        try {
            
            session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
        
            session.delete(instance);
            session.getTransaction().commit();
                    
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            if(session != null) {
                session.flush();
                session.close();
            }
        }
    }
    
    public final void deleteByObjid(Serializable id) throws BaseException {

        if (id == null) return;
        
        Session session = null;
        try {
        
            session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();

            Query hql = session.createQuery(" from " + clazz.getName() + " where objid = :id ");
            hql.setParameter("id", id);

            List list = hql.list();
            if (list != null && !list.isEmpty()) {
                T instance = (T) list.get(0);
                session.delete(instance);
                session.getTransaction().commit();
            }

        } catch (Exception ex) {
            
        } finally {
            if(session != null) {
                session.flush();
                session.close();
            }
        }
        
    }

}
